package org.an.proj.contacts.ui.contact.view;

import android.content.Context;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import org.an.ku.view.MyTextView;
import org.an.proj.contacts.R;

/**
 * 创建时间：2016年10月18日13:20 <br>
 * 作者：fuchaoyang <br>
 * 描述：
 */

public class ContactItemBlockView extends RelativeLayout {
  @Bind(R.id.tv_picker) public MyTextView mTvPicker;
  @Bind(R.id.edt_input) public EditText mEdtInput;
  @Bind(R.id.btn_del) public View mBtnDel;
  // 用于区分是什么
  private String mTag;

  private IOnDeleteListener mOnDeleteListener;

  public ContactItemBlockView(Context context) {
    this(context, null);
  }

  public ContactItemBlockView(Context context, AttributeSet attrs) {
    super(context, attrs);
    View view = View.inflate(getContext(), R.layout.view_contact_item, this);
    ButterKnife.bind(this, view);
  }

  public void setTag(String tag) {
    mTag = tag;
  }

  @Override public String getTag() {
    return mTag;
  }

  public void setOnDeleteListener(IOnDeleteListener listener) {
    this.mOnDeleteListener = listener;
  }

  /**
   * 设置title和hint
   */
  public ContactItemBlockView setData(String title, String hint) {
    mTvPicker.setText(title);
    mEdtInput.setHint(hint);
    return this;
  }

  /**
   * {@link EditorInfo#inputType}
   */
  public ContactItemBlockView setInputType(int type) {
    mEdtInput.setInputType(type);
    return this;
  }

  public ContactItemBlockView addTextChangedListener(TextWatcher watcher) {
    mEdtInput.addTextChangedListener(watcher);
    return this;
  }

  //public ContactItemBlockView setHint(String hint) {
  //  mEdtInput.setHint(hint);
  //  return this;
  //}
  //
  //public ContactItemBlockView setTitle(String title) {
  //  mTvPicker.setText(title);
  //  return this;
  //}

  /**
   * 设置是否可编辑
   */
  public ContactItemBlockView setEditable(boolean editable) {
    mEdtInput.setFocusable(editable);
    mEdtInput.setFocusableInTouchMode(editable);
    return this;
  }

  public void clearEdit() {
    mEdtInput.setText("");
  }

  /**
   * 如果是有多个，可以给view设置tag为position
   *
   * @param view view.setTag(position)
   */
  @OnClick(R.id.btn_del) public void onDelete(View view) {
    if (mOnDeleteListener != null) {
      mOnDeleteListener.onDelete(this, view);
    }
  }

  public interface IOnDeleteListener {
    /**
     *
     * @param parent
     * @param btnDel
     */
    void onDelete(ContactItemBlockView parent, View btnDel);
  }
}
